using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACKETPARSERMAX20.CORE;

namespace PACKETPARSERMAX20.CORE.DEFINITIONS
{
    public class SMSG_QUEST_QUERY_RESPONSE_DEF : DefinitionBase
    {

        public override bool Process()
        {
            quest_template template = GetQuestTemplate();
            GlobalCache.AddQuestTemplateSum(template, ClientBuild);
            return Validate();
        }

        public quest_template GetQuestTemplate()
        {
            ResetPosition();

            quest_template template = new quest_template();

            template.entry = ReadUInt32();
            template.method = (byte)ReadUInt32();               // Accepted values: 0, 1 or 2. 0==IsAutoComplete() (skip objectives/details)
            template.questlevel = (short)ReadInt32();                // may be 0
            if (ClientBuild > 10505)// 10482)
            {
                template.minlevel = (byte)ReadInt32();
            }
            template.zoneorsort = (short)ReadUInt32();                // zone or sort to display in quest log
            template.type = (ushort)ReadUInt32();
            template.suggestedplayers = (byte)ReadUInt32();

            template.repobjectivefaction = (ushort)ReadUInt32();       // shown in quest log as part of quest objective
            template.repobjectivevalue = (int)ReadUInt32();         // shown in quest log as part of quest objective

            var requiredopositerepfaction = ReadUInt32();                                   // RequiredOpositeRepFaction
            var requiredopositerepvalue = ReadUInt32();                                      // RequiredOpositeRepValue, required faction value with another (oposite) faction (objective)

            template.nextquestinchain = ReadUInt32();            // client will request this quest from NPC, if not 0

            if (ClientBuild >= 10958)
            {
                template.rewxpid = (byte)ReadUInt32();
            }

            template.reworreqmoney = (int)ReadUInt32();   // Hide money rewarded
            template.rewmoneymaxlevel = ReadUInt32();           // used in XP calculation at client
            template.rewspell = ReadUInt32();                    // reward spell, this spell will display (icon) (casted if RewSpellCast==0)
            template.rewspellcast = ReadUInt32();             // casted spell

            if (ClientBuild >= 7561)
            {
                template.rewhonoraddition = ReadUInt32(); // rewarded honor points 2.3.0, Honor points
            }

            if (ClientBuild >= 10958)
            {
                template.rewhonormultiplier = ReadFloat();
            }

            template.srcitemid = ReadUInt32();
            template.questflags = ReadUInt32();

            __QuestFlags questflags = (__QuestFlags)template.questflags;

            if (ClientBuild >= 7561)
            {
                template.chartitleid = (byte)ReadUInt32();                 // CharTitleId, new 2.4.0, player gets this title (id from CharTitles)
            }

            if (ClientBuild >= 9183)
            {
                template.playersslain = (byte)ReadUInt32();               // players slain
                template.bonustalents = (byte)ReadUInt32();               // bonus talents
            }

            if (ClientBuild >= 10958)
            {
                var bonusarenapoints = ReadUInt32();                                       // bonus arena points
                var rewrepshowmask = ReadUInt32();                                       // rew rep show mask?            }
            }

            template.rewitemid1 = ReadUInt32();
            template.rewitemcount1 = (ushort)ReadUInt32();

            template.rewitemid2 = ReadUInt32();
            template.rewitemcount2 = (ushort)ReadUInt32();

            template.rewitemid3 = ReadUInt32();
            template.rewitemcount3 = (ushort)ReadUInt32();

            template.rewitemid4 = ReadUInt32();
            template.rewitemcount4 = (ushort)ReadUInt32();


            template.rewchoiceitemid1 = ReadUInt32();
            template.rewchoiceitemcount1 = (ushort)ReadUInt32();

            template.rewchoiceitemid2 = ReadUInt32();
            template.rewchoiceitemcount2 = (ushort)ReadUInt32();

            template.rewchoiceitemid3 = ReadUInt32();
            template.rewchoiceitemcount3 = (ushort)ReadUInt32();

            template.rewchoiceitemid4 = ReadUInt32();
            template.rewchoiceitemcount4 = (ushort)ReadUInt32();

            template.rewchoiceitemid5 = ReadUInt32();
            template.rewchoiceitemcount5 = (ushort)ReadUInt32();

            template.rewchoiceitemid6 = ReadUInt32();
            template.rewchoiceitemcount6 = (ushort)ReadUInt32();

            if (ClientBuild >= 10958)
            {
                // reward factions ids
                template.rewrepfaction1 = (ushort)ReadUInt32();
                template.rewrepfaction2 = (ushort)ReadUInt32();
                template.rewrepfaction3 = (ushort)ReadUInt32();
                template.rewrepfaction4 = (ushort)ReadUInt32();
                template.rewrepfaction5 = (ushort)ReadUInt32();

                // columnid in QuestFactionReward.dbc (if negative, from second row)
                template.rewrepvalueid1 = (sbyte)ReadInt32();
                template.rewrepvalueid2 = (sbyte)ReadInt32();
                template.rewrepvalueid3 = (sbyte)ReadInt32();
                template.rewrepvalueid4 = (sbyte)ReadInt32();
                template.rewrepvalueid5 = (sbyte)ReadInt32();

                // reward reputation override. No bonus is expected given
                // current field for store of rep value, can be reused to implement "override value"
                var rewardreputationoverride1 = (sbyte)ReadInt32();
                var rewardreputationoverride2 = (sbyte)ReadInt32();
                var rewardreputationoverride3 = (sbyte)ReadInt32();
                var rewardreputationoverride4 = (sbyte)ReadInt32();
                var rewardreputationoverride5 = (sbyte)ReadInt32();

            }

            template.pointmapid = (ushort)ReadUInt32();
            template.pointx = ReadSingle();
            template.pointy = ReadSingle();
            template.pointopt = ReadUInt32();

            template.title = ReadString();
            template.objectives = ReadString();
            template.details = ReadString();
            template.endtext = ReadString();

            if (ClientBuild > 10505)// 10482)
            {
                template.completedtext = ReadString();
            }

            if (ClientBuild >= 9183)
            {
                template.reqcreatureorgoid1 = (int)ReadUInt32();
                template.reqcreatureorgocount1 = (ushort)ReadUInt32();
                template.reqsourceid1 = ReadUInt32();
                if (ClientBuild > 10505)// 10482)
                {
                    var reqsourcecount1 = ReadUInt32();
                }
                template.reqcreatureorgoid2 = (int)ReadUInt32();
                template.reqcreatureorgocount2 = (ushort)ReadUInt32();
                template.reqsourceid2 = ReadUInt32();
                if (ClientBuild > 10505)// 10482)
                {
                    var reqsourcecount2 = ReadUInt32();
                }

                template.reqcreatureorgoid3 = (int)ReadUInt32();
                template.reqcreatureorgocount3 = (ushort)ReadUInt32();
                template.reqsourceid3 = ReadUInt32();
                if (ClientBuild > 10505)// 10482)
                {
                    var reqsourcecount3 = ReadUInt32();
                }

                template.reqcreatureorgoid4 = (int)ReadUInt32();
                template.reqcreatureorgocount4 = (ushort)ReadUInt32();
                template.reqsourceid4 = ReadUInt32();
                if (ClientBuild > 10505)// 10482)
                {
                    var reqsourcecount4 = ReadUInt32();
                }

                template.reqitemid1 = ReadUInt32();
                template.reqitemcount1 = (ushort)ReadUInt32();

                template.reqitemid2 = ReadUInt32();
                template.reqitemcount2 = (ushort)ReadUInt32();

                template.reqitemid3 = ReadUInt32();
                template.reqitemcount3 = (ushort)ReadUInt32();

                template.reqitemid4 = ReadUInt32();
                template.reqitemcount4 = (ushort)ReadUInt32();

                if (ClientBuild > 9183)
                {
                    template.reqitemid5 = ReadUInt32();
                    template.reqitemcount5 = (ushort)ReadUInt32();

                    if (ClientBuild >= 10314)
                    {
                        template.reqitemid6 = ReadUInt32();
                        template.reqitemcount6 = (ushort)ReadUInt32();
                    }
                }
                //if (ClientBuild > 9183)
                //{
                //    ReadUInt32(); //ReqItemIdExt
                //    template.reqitemcount5 = (ushort)ReadUInt32();

                //    if (ClientBuild >= 10314)
                //    {
                //        ReadUInt32(); //ReqItemIdExt
                //        template.reqitemcount6 = (ushort)ReadUInt32();
                //    }
                //}

            }
            else
            {
                template.reqcreatureorgoid1 = (int)ReadUInt32();
                template.reqcreatureorgocount1 = (ushort)ReadUInt32();
                template.reqitemid1 = ReadUInt32();
                template.reqitemcount1 = (ushort)ReadUInt32();

                template.reqcreatureorgoid2 = (int)ReadUInt32();
                template.reqcreatureorgocount2 = (ushort)ReadUInt32();
                template.reqitemid2 = ReadUInt32();
                template.reqitemcount2 = (ushort)ReadUInt32();

                template.reqcreatureorgoid3 = (int)ReadUInt32();
                template.reqcreatureorgocount3 = (ushort)ReadUInt32();
                template.reqitemid3 = ReadUInt32();
                template.reqitemcount3 = (ushort)ReadUInt32();

                template.reqcreatureorgoid4 = (int)ReadUInt32();
                template.reqcreatureorgocount4 = (ushort)ReadUInt32();
                template.reqitemid4 = ReadUInt32();
                template.reqitemcount4 = (ushort)ReadUInt32();
            }

            template.objectivetext1 = ReadString();
            template.objectivetext2 = ReadString();
            template.objectivetext3 = ReadString();
            template.objectivetext4 = ReadString();

            return template;
        }

    }
}
